package com.core.db.require;

import java.sql.SQLException;
import java.util.List;

import com.core.db.condition.Condition;

class Insert extends BaseRequire
{
	private List< ColumnDescribe > describes;
	private String table;

	public Insert( String table, List< ColumnDescribe > describes )
	{
		this.table = table;
		this.describes = describes;
	}

	@Override
	public String sql() throws SQLException
	{
		if ( table == null || table.length() == 0 )
		{
			throw new SQLException( "No table to be found to be operated on" );
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append( "INSERT INTO" ).append( " " );
		buffer.append( table ).append( " " );
		buffer.append( assembleColumns( getColumns() ) );
		return buffer.toString();
	}

	@Override
	public List< ColumnDescribe > getColumnDescribes()
	{
		return this.describes;
	}
	
	@Override
	protected String assembleColumns( List< String > columns )
	{
		StringBuffer buffer = new StringBuffer();
		if ( columns != null && columns.size() > 0 )
		{
			buffer.append( "(" );
			int maxIndex = columns.size() - 1;
			for ( int i = 0; i < maxIndex; i++ )
			{
				buffer.append( columns.get( i ) ).append( "," );
			}
			buffer.append( columns.get( maxIndex ));
			buffer.append( ")" );
			buffer.append( " VALUES(" );
			for ( int i = 0; i < maxIndex; i++ )
			{
				buffer.append( "?" ).append( "," );
			}
			buffer.append( "?" );
			buffer.append( ")" );
		}
		return buffer.toString();
	}

	@Override
	public Condition getCondition()
	{
		return null;
	}

}
